"""
Pydantic 数据模型（用于API请求和响应验证）
"""
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime


# ========== 作品相关 ==========
class WorkImageBase(BaseModel):
    """作品图片基础模型"""
    image_url: str
    sort_order: int = 0


class WorkImageCreate(WorkImageBase):
    """创建作品图片"""
    pass


class WorkImageResponse(WorkImageBase):
    """作品图片响应模型"""
    id: int
    work_id: int
    created_at: datetime
    
    class Config:
        from_attributes = True


class WorkBase(BaseModel):
    """作品基础模型"""
    title: str
    subtitle: str
    cover_image: Optional[str] = None
    description: Optional[str] = None


class WorkCreate(WorkBase):
    """创建作品请求模型"""
    images: List[WorkImageCreate] = []


class WorkUpdate(BaseModel):
    """更新作品请求模型"""
    title: Optional[str] = None
    subtitle: Optional[str] = None
    cover_image: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None


class WorkResponse(WorkBase):
    """作品响应模型"""
    id: int
    created_at: datetime
    updated_at: datetime
    is_active: bool
    images: List[WorkImageResponse] = []
    
    class Config:
        from_attributes = True


class WorkListResponse(BaseModel):
    """作品列表响应模型（简化版，用于首页）"""
    id: int
    title: str
    subtitle: str
    cover_image: Optional[str] = None
    
    class Config:
        from_attributes = True


# ========== 用户相关 ==========
class UserBase(BaseModel):
    """用户基础模型"""
    openid: str
    nickname: Optional[str] = None
    avatar: Optional[str] = None


class UserCreate(UserBase):
    """创建用户请求模型"""
    pass


class UserResponse(UserBase):
    """用户响应模型"""
    id: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


# ========== 收藏相关 ==========
class FavoriteCreate(BaseModel):
    """创建收藏请求模型"""
    user_id: int
    work_id: int


class FavoriteResponse(BaseModel):
    """收藏响应模型"""
    id: int
    user_id: int
    work_id: int
    created_at: datetime
    work: WorkListResponse  # 包含作品信息
    
    class Config:
        from_attributes = True


# ========== 客片相关 ==========
class GalleryItemResponse(BaseModel):
    """客片列表项响应模型"""
    id: int
    image_url: str
    title: str
    subtitle: str
    work_id: int
    
    class Config:
        from_attributes = True

